package com.emperor.service.impl;

import com.emperor.dto.SkuSaveDto;
import com.emperor.entity.Sku;
import com.emperor.entity.SkuClass;
import com.emperor.mapper.SkuClassMapper;
import com.emperor.mapper.SkuMapper;
import com.emperor.service.SkuService;
import com.emperor.vo.SkuClassPageVo;
import com.emperor.vo.SkuVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SkuServiceImpl implements SkuService {
    @Autowired
    private SkuMapper skuMapper;


    @Override
    public void save(SkuSaveDto dto) {
        Sku sku = new Sku();
        BeanUtils.copyProperties(dto, sku);
        skuMapper.save(sku);
    }

    @Override
    public SkuClassPageVo findAll(Integer pageIndex, Integer pageSize, String className) {
        PageHelper.startPage(pageIndex, pageSize);
        Page<SkuVo> page = (Page<SkuVo>) skuMapper.findAll(className);
        System.out.println(page.getResult());
        long l = (page.getTotal() / pageSize) + 1;
        SkuClassPageVo build = SkuClassPageVo.builder().
                pageIndex(pageIndex).
                pageSize(pageSize)
                .totalPage((int) l).totalCount((int) page.getTotal())
                .currentPageRecords(page.getResult()).build();
        return build;
    }

    @Override
    public void updateById(String skuId, SkuSaveDto dto) {
        skuMapper.updateById(skuId, dto);
    }
}
